﻿Imports System.Windows.Forms
Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Net
Imports System.IO.Compression

Public Class 请求好友
    Public _uid As String = ""
    Public _id As String = ""
End Class

Public Class UTIL
    Public Shared Function ReadStreamEncodingContent(ByVal wbres As HttpWebResponse) As String

        If wbres Is Nothing Then
            Return ""
        End If

        Dim html As String
        Try
            If wbres.ContentEncoding.ToLower.Contains("gzip") Then
                Dim responseStream As GZipStream = New GZipStream(wbres.GetResponseStream(), CompressionMode.Decompress)
                Dim streamReader As StreamReader = New StreamReader(responseStream)
                html = streamReader.ReadToEnd()
                responseStream.Close()
                streamReader.Close()
            ElseIf wbres.ContentEncoding.ToLower.Contains("deflate") Then
                Dim responseStream As DeflateStream = New DeflateStream(wbres.GetResponseStream(), CompressionMode.Decompress)
                Dim streamReader As StreamReader = New StreamReader(responseStream)
                html = streamReader.ReadToEnd()
                responseStream.Close()
                streamReader.Close()
            Else
                Dim streamReader As StreamReader = New StreamReader(wbres.GetResponseStream())
                html = streamReader.ReadToEnd()
                streamReader.Close()
            End If

            Return html

        Catch ex As Exception
            Return ""
        End Try

        Return ""

    End Function

    '<p><a href="(?<URL>space-(?<id>\d{5,11}).html)" title="(?<名称>[\S\S]+?)">(?<姓名>[\S\S]+?)</a></p>
    'html"><img src="(?<图片>[\s\S]+?)" alt="[\S\s]+?" /></a></div>[\W\w]+?<p><a href="(?<URL>space-(?<id>\d{5,11}).html)" title="[\S\S]+?">(?<姓名>[\S\S]+?)</a></p>
    'http://hi.csdn.net/
    '<strong class="index_name">[\s\S]+? <em>\(用户名:(?<用户名>[\s\S]+?)\)</em></strong> 分析用户名
    Public Shared Function ParseBlogContentUrl(ByVal strContent As String) As List(Of MyFriend)

        Dim lst As New List(Of MyFriend)
        Dim mcs As MatchCollection = Regex.Matches(strContent, "html""><img src=""(?<图片>[\s\S]+?)"" alt=""[\S\s]+?"" /></a></div>[\W\w]+?<p><a href=""(?<URL>space-(?<id>\d{5,11}).html)"" title=""[\S\S]+?"">(?<姓名>[\S\S]+?)</a></p>")
        Dim mc1 As Match = Regex.Match(strContent, "<strong class=""index_name"">[\s\S]+? <em>\(用户名:(?<用户名>[\s\S]+?)\)</em></strong>")
        For Each mc As Match In mcs
            Dim item As New MyFriend
            item.Url = "http://hi.csdn.net/" + mc.Groups("URL").Value
            item.id = mc.Groups("id").Value
            item.name = mc.Groups("姓名").Value
            If mc1 IsNot Nothing AndAlso mc1.Groups.Count = 3 Then
                item.username = mc1.Groups("用户名").Value
            Else
                item.username = mc.Groups("姓名").Value
            End If

            lst.Add(item)
        Next

        Return lst

    End Function
    '<a href="cp.php\?ac=friend&op=ignore&uid=(?<ID1>[\S\s]+?)&confirm=1" id="(?<ID>[\s\S]*?)" (onclick="ajaxmenu\(event, this.id, 99999, '', -1\)" class="submit a">批准申请</a>)*?
    Public Shared Function 分析要请求的好友(ByVal strContent As String, ByRef lst请求好友 As List(Of 请求好友)) As Boolean
        Dim strPattern As String = "<a href=""cp.php\?ac=friend&op=ignore&uid=(?<ID1>[\S\s]+?)&confirm=1"" id=""(?<ID>[\s\S]*?)"" (onclick=""ajaxmenu\(event, this.id, 99999, '', -1\)"" class=""submit a"">批准申请</a>)*?"
        Dim mcs As MatchCollection = Regex.Matches(strContent, strPattern)
        For Each mc As Match In mcs
            Dim temp As New 请求好友
            temp._uid = mc.Groups("ID1").Value
            temp._id = mc.Groups("ID").Value
            lst请求好友.Add(temp)
        Next

        Return True

    End Function

    Public Shared Function ParseUsername(ByVal strContent As String, ByRef username As String) As Boolean
        Dim mc1 As Match = Regex.Match(strContent, "<strong class=""index_name"">[\s\S]+? <em>\(用户名:(?<用户名>[\s\S]+?)\)</em></strong>")
        If mc1 IsNot Nothing AndAlso mc1.Groups.Count = 2 Then
            username = mc1.Groups("用户名").Value
            Return True
        End If

        Return False

    End Function

    'Dim formHashPattern As String = "<input type=""hidden"" name=""formhash"" value=""(?<formhash>[\s\S]+?)"" />"
    Public Shared Function GetFormHash(ByVal strContent As String, ByRef formHash As String) As Boolean
        Dim formHashPattern As String = "<input type=""hidden"" name=""formhash"" value=""(?<formhash>[\s\S]+?)"" />"
        Dim match As Match = Regex.Match(strContent, formHashPattern)
        If match IsNot Nothing AndAlso match.Groups.Count = 2 Then
            formHash = match.Groups("formhash").Value
            Return True
        End If

        Return False

    End Function

    '<root><!\[CDATA\[[\S\s]+?>(?<内容>[\S\s]+?)</a>[\s\S]+?</root>
    Public Shared Function Get发送完关注返回内容(ByVal strContent As String, ByRef strRs As String) As Boolean
        Dim formHashPattern As String = "<root><!\[CDATA\[[\S\s]+?>(?<内容>[\S\s]+?)</a>[\s\S]+?</root>"
        Dim match As Match = Regex.Match(strContent, formHashPattern)
        If match IsNot Nothing AndAlso match.Groups.Count = 2 Then
            strRs = match.Groups("内容").Value
            Return True
        End If

        Return False

    End Function

    '\<root\>\<\!\[CDATA\[(?<内容>[\S\s]+?)\]\]\>\</root\>
    Public Shared Function Get发送完留言返回内容(ByVal strContent As String, ByRef strRs As String) As Boolean
        Dim formHashPattern As String = "<root><!\[CDATA\[(?<内容>[\S\s]+?)\]\]></root>"
        Dim match As Match = Regex.Match(strContent, formHashPattern)
        If match IsNot Nothing AndAlso match.Groups.Count = 2 Then
            strRs = match.Groups("内容").Value
            Return True
        End If

        Return False

    End Function

    '<div class="popupmenu_inner">(?<内容>[\s\S]+?)</div>
    Public Shared Function Get添加关注返回内容(ByVal strContent As String, ByRef strRs As String) As Boolean
        Dim formHashPattern As String = "<div class=""popupmenu_inner"">(?<内容>[\s\S]+?)</div>"
        Dim match As Match = Regex.Match(strContent, formHashPattern)
        If match IsNot Nothing AndAlso match.Groups.Count = 2 Then
            strRs = match.Groups("内容").Value
            Return True
        End If

        Return False

    End Function

    Public Shared Function GetUserName(ByVal strImageUrl As String) As String
        Dim intFindStart As Integer = strImageUrl.LastIndexOf("_")
        Return strImageUrl.Substring(intFindStart + 1, strImageUrl.Length - intFindStart - 1 - 4)
    End Function

    '2011 03 03 20 18 15
    Public Shared Function Get两个时间间隔(ByVal DateF As String, ByVal DataT As String) As Decimal
        Dim DataF年 As Integer = CInt(DateF.Substring(0, 4))
        Dim DataF月 As Integer = CInt(DateF.Substring(4, 2))
        Dim DataF日 As Integer = CInt(DateF.Substring(6, 2))
        Dim DataF时 As Integer = CInt(DateF.Substring(8, 2))
        Dim DataF分 As Integer = CInt(DateF.Substring(10, 2))
        Dim DataF秒 As Integer = CInt(DateF.Substring(12, 2))

        Dim DataT年 As Integer = CInt(DataT.Substring(0, 4))
        Dim DataT月 As Integer = CInt(DataT.Substring(4, 2))
        Dim DataT日 As Integer = CInt(DataT.Substring(6, 2))
        Dim DataT时 As Integer = CInt(DataT.Substring(8, 2))
        Dim DataT分 As Integer = CInt(DataT.Substring(10, 2))
        Dim DataT秒 As Integer = CInt(DataT.Substring(12, 2))

        Dim newDateF As New Date(DataF年, DataF月, DataF日, DataF时, DataF分, DataF秒)
        Dim newDateT As New Date(DataT年, DataT月, DataT日, DataT时, DataT分, DataT秒)

        Return newDateF.Subtract(newDateT).Seconds

    End Function
End Class
